package com.aefottt.struggle_star.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * 获取格式化的时间信息
     * @param time 毫秒
     * @return xx年xx月xx日
     */
    public static String getFormattedTime(long time){
        return sdf.format(time);
    }

    /**
     * 获取时间对应的年份
     */
    public static int getYearOfTime(long time){
        return Integer.parseInt(sdf.format(time).substring(0, 4));
    }

    /**
     * 获取时间对应的月份
     */
    public static int getMonthOfTime(long time){
        return Integer.parseInt(sdf.format(time).substring(5, 7));
    }

    /**
     * 获取时间对应的天
     */
    public static int getDayOfTime(long time){
        return Integer.parseInt(sdf.format(time).substring(8, 10));
    }

    /**
     * 获取时间对应的小时和分钟
     */
    public static String getHourAndMinuteOfTime(long time){
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH:MM");
        return sdf2.format(time).substring(11);
    }

    /**
     * 将年月日转换为对应的时间戳
     * @return millis
     */
    public static long getMillis(int year, int month, int day){
        long millis = 0;
        try {
            Date date = sdf.parse(year+"年"+month+"月"+day+"日");
            if (date != null) {
                millis = date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millis;
    }

    /**
     * 将年月日时分转换为对应的时间戳
     * @return millis
     */
    public static long getMillis(int year, int month, int day, int hour, int minute){
        long millis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:MM");
        try {
            Date date = sdf.parse(year+"年"+month+"月"+day+"日"+hour+":"+minute);
            if (date != null) {
                millis = date.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millis;
    }

    /**
     * 获取两个时间相隔多少天
     */
    public static int getDaysGap(long time1, long time2){
        return (getYearOfTime(time1) - getYearOfTime(time2))*365
                + (getMonthOfTime(time1) - getMonthOfTime(time2))*30
                + getDayOfTime(time1) - getDayOfTime(time2);
    }
}
